package com.wanmait.gohome.controller.manage;

import com.wanmait.gohome.pojo.Admin;
import com.wanmait.gohome.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manage")
public class ManageLoginController {
    @Resource
    private AdminService adminService;
    //登陆界面
    @RequestMapping("login")
    public void login(){}
   //登陆表单提交
    @PostMapping("login")
    public String login(String account, String password,HttpServletRequest request){
        Admin admin = adminService.findByAccountAndPass(account,password,request);
        Admin info = adminService.findInfo(account);
        Integer mark = adminService.findMark(account);
        if(mark==0){
            HttpSession session = request.getSession();
            session.setAttribute("info",info.getDeleteReason());
            return "redirect:/manage/login?err=MarkErr";
        }
        if(admin!=null){
            HttpSession session = request.getSession();
            session.setAttribute("info",info);
            session.setAttribute("admin",admin);
            return "redirect:/manage/manageIndex";
        }
        else{
            return "redirect:/manage/login?err=adminErr";

        }
    }

    //检查登陆验证码是否正确
    @ResponseBody
    @GetMapping("checkCode")
     public String checkCode(String code,HttpServletRequest request){
        String rightCode = (String)request.getSession().getAttribute("randcode");
        if(code==null||!code.equalsIgnoreCase(rightCode)){

            return "false";//验证码不正确
        }else{
            return "true";             //验证码正确
        }
    }
    @GetMapping("exit")
    public String exit(HttpServletRequest request,Integer id){
        request.getSession().removeAttribute("admin");
        adminService.cleanLoginIp(id);
        return "redirect:/manage/login";
    }
    }
